package com.pay.service.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.pay.common.util.PropertyUtil;
import com.pay.service.WxBankCardNoService;

@Service("wxBankCardNoService")
public class WxBankCardNoServiceImpl implements WxBankCardNoService{
	private Log log = LogFactory.getLog(WxBankCardNoServiceImpl.class);
	private static String url ;
	private static String [] supportBankCodes ;

	static{
		PropertyUtil propertyUtil = PropertyUtil.getInstance("system");
		url = propertyUtil.getProperty("bankcardno.recognize.url") ;
		supportBankCodes = propertyUtil.getProperty("ccpb.support.bank").split(",") ;
	}
	/**
	 * 获取银行卡的详细信息
	 * 格式：{"code":"406252","length":6,"panLength":16,"providerCode":"CEB","cardType":"CREDIT","cardName":"阳光信用卡(银联","agencyCode":"63030000","agencyName":"中国光大银行"}
	 * @throws Exception
	 */
	@Override
	public String getBankCardInfo(String creditCardNo) throws Exception  {
		log.info("creditCardNo" + creditCardNo);
		String response = null ;
		StringBuffer params = new StringBuffer();
		params.append(creditCardNo)
			.append("&fields=code")
			.append("&fields=length")
			.append("&fields=panLength")
			.append("&fields=providerCode")
			.append("&fields=cardType")
			.append("&fields=cardName")
			.append("&fields=agencyCode")
			.append("&fields=agencyName") ;
		String requestUrl = url + params.toString();
		response = request(requestUrl) ;
		log.info("bankcard2-response:" + response);
		if(response != null){
			return response ;
		} else{
			throw new Exception("31  params:" + params.toString());
		}
	}
	private String request(String url) throws Exception{
		log.info("bankcard-request:" + url);
		String result = null ;
		HttpURLConnection conn = null ;
    	try{
    		URL u = new URL(url);
        	conn = (HttpURLConnection) u.openConnection();
    		conn.setConnectTimeout(5 * 1000);
    		conn.setReadTimeout(5 * 1000);
    		conn.setRequestMethod("GET");
    		conn.setRequestProperty("Charset", "UTF-8");
    		conn.connect();
    		if (conn.getResponseCode() == 200) {
    			//接受返回数据
                InputStream inStream = conn.getInputStream();
                result = convertStreamToString(inStream);
    		}else{
    			throw new Exception("HttpURLConnection,getResponseCode:" + conn.getResponseCode());
    		}
    	} catch(Exception e){
    		throw e;
    	} finally{
        	if(conn != null){
        		conn.disconnect();
        	}
        }

    	return result ;
	}
	private static String convertStreamToString(InputStream is) throws Exception{
		if(is == null)return null ;

		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			throw e;
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return sb.toString();
	}
	@Override
	public String isSupportCreditCardNo(String creditCardNo) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
